/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ar.web;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ar.dao.TarDao;
import com.jeesite.modules.ar.entity.Tar;
import com.jeesite.modules.ar.entity.TarDetail;
import com.jeesite.modules.ar.service.TarService;
import com.jeesite.modules.arecieve.entity.TreceivableDetail;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.fixedexpense.FixedExpense;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.fixedexpense.FixedExpenseService;
import com.jeesite.modules.financing.service.finserviceorder.YYService;
import com.jeesite.modules.order.dao.serviceorder.ServiceOrderDetailDao;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.pay.entity.TpaymentDetail;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.payment.entity.TapDetail;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 应收单Controller
 * @author YY
 * @version 2021-03-12
 */
@Controller
@RequestMapping(value = "${adminPath}/ar/tar")
public class TarController extends BaseController {

	@Autowired
	private TarService tarService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private ServiceOrderDetailDao serviceOrderDetailDao;
	@Autowired
	private TarDao tarDao;
	@Autowired
	private FixedExpenseService fixedExpenseService;
	@Autowired
	private YYService yyService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Tar get(String arId, boolean isNewRecord) {
		return tarService.get(arId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ar:tar:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tar tar, Model model) {
		model.addAttribute("tar", tar);
		model.addAttribute("listAr",tarService.findList(new Tar()));//应收单列表
		model.addAttribute("listCu",customerService.findList(new Customer()));//客户列表
		return "modules/ar/tarList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ar:tar:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Tar> listData(Tar tar, HttpServletRequest request, HttpServletResponse response) {

		tar.setPage(new Page<>(request, response));
		Page<Tar> page = tarService.findPage(tar);
		for (Tar tar1 : page.getList()) {
			/*if(tar1.getParentBillType()!=null&&
					!"".equals(tar1.getParentBillType())
					&&tar1.getParentBillCode()!=null&&!"".equals(tar1.getParentBillCode())) {
				String billCodeById = yyService.getBillCodeById(tar1.getParentBillType(), tar1.getParentBillCode());
				tar1.setParentBillCode1(tar1.getParentBillCode());
				tar1.setParentBillCode(billCodeById);
			}*/
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ar:tar:view")
	@RequestMapping(value = "form")
	public String form(Tar tar, Model model) throws ParseException {
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		/*tar.setCreateDate(simpleDateFormat.parse(simpleDateFormat.format(tar.getCreateDate())));
		tar.setUpdateDate(simpleDateFormat.parse(simpleDateFormat.format(tar.getUpdateDate())));*/
		if(tar.getIsNewRecord()){
			model.addAttribute("rowNumberList", ListUtils.newArrayList());
		} else {
			List<TarDetail> rowNumberList =new ArrayList<>();
			for (TarDetail tarDetail : tar.getTarDetailList()) {
				rowNumberList.add(tarDetail);
			}
			model.addAttribute("rowNumberList", rowNumberList);
		}
		if(!tar.getIsNewRecord()){
			List<ServiceOrder> serviceOrderList =new ArrayList<>();
			ServiceOrder serviceOrder =new ServiceOrder();
			serviceOrder.setServiceOrderId(tar.getServiceOrderId());
			serviceOrder=serviceOrderService.get(serviceOrder);
			serviceOrderList.add(serviceOrder);

			model.addAttribute("serviceOrderList",serviceOrderList);
		}else{
			model.addAttribute("serviceOrderList", ListUtils.newArrayList());
		}
		model.addAttribute("tar", tar);
		model.addAttribute("listAr",tarService.findList(new Tar()));//应收单列表
		model.addAttribute("listCu",customerService.findList(new Customer()));//客户列表
        model.addAttribute("fixedExpense",fixedExpenseService.findList(new FixedExpense()));
		return "modules/ar/tarForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("ar:tar:view")
	@RequestMapping(value = "detail")
	public String detail(Tar tar, Model model) {
		if(tar.getParentBillType()!=null&&tar.getParentBillCode()!=null&&!"".equals(tar.getParentBillType())&&!"".equals(tar.getParentBillCode())) {
		String billCodeById = yyService.getBillCodeById(tar.getParentBillType(), tar.getParentBillCode());
			tar.setParentBillCode1(billCodeById);
	}
		model.addAttribute("tar", tar);
		return "modules/ar/tarDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ar:tar:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Tar tar) {
		tarService.save(tar);
		return renderResult(Global.TRUE, text("保存应收单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ar:tar:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Tar tar) {
		tarService.delete(tar);
		return renderResult(Global.TRUE, text("删除应收单成功！"));
	}

	/**
	 * 获取通过选定客户查询应收单部分信息
	 * @param customerId  客户id
	 */
	@RequestMapping(value = "getTarByCuId")
	@ResponseBody
	public List<ServiceOrder> getServiceOrderId(@RequestParam("customerId") String customerId) {
		List<ServiceOrder> strings = tarDao.selectServiceOrder(customerId);
		return strings;
	}

	/**
	 * 通过订单号，得到币种
	 * @param serviceOrderId  订单号
	 */
	@RequestMapping(value = "getCurrencyOrderId")
	@ResponseBody
	public List<ServiceOrderDetail> getCurrencyOrderId(@RequestParam("serviceOrderId") String serviceOrderId) {
		List<ServiceOrderDetail> serviceOrderDetailList = serviceOrderService.get(new ServiceOrder(serviceOrderId)).getServiceOrderDetailList();
		return serviceOrderDetailList;
	}
	/**
	 * 通过详情id，得到基本信息对象
	 * @param tarDeatilId  订单号id
	 *
	 */
	@RequestMapping(value = "getTarDetailByDetailId")
	@ResponseBody
	public ServiceOrderDetail getTarDetailByDetailId(@RequestParam("tarDeatilId") String tarDeatilId) {
		ServiceOrderDetail serviceOrderDetail=new ServiceOrderDetail();
		serviceOrderDetail.setServiceOrderDetailId(tarDeatilId);
		ServiceOrderDetail serviceOrderDetail1 = serviceOrderDetailDao.findList(serviceOrderDetail).get(0);
		return serviceOrderDetail1;
	}

	/**
	 * 通过arid修改状态
	 * @param arId  订单号id
	 *
	 */
	@RequestMapping(value = "updateFinancialRecognition")
	@ResponseBody
	public String updateFinancialRecognition(@RequestParam("arId") String arId) {
		Tar tar=new Tar();
		tar.setArId(arId);
		Tar tar1 = tarService.get(tar);
		tarDao.updataStatus1(arId);
        return "实收金额："+tar1.getAmount();
	}
	/**
	 * 查询符合的预收单
	 */
	@RequiresPermissions("ar:tar:view")
	@RequestMapping(value = "getTarList")
	@ResponseBody
	public List<TreceivableDetail> getTapList(Tar tar){

		List<Tar> tarList =tarService.findList(tar);
		List<TreceivableDetail> treceivableDetailList =new ArrayList<>();
		for (Tar tar1 : tarList) {
			tar1 =tarService.get(tar1);
			TreceivableDetail treceivableDetail =null;
			for (TarDetail tarDetail : tar1.getTarDetailList()) {
				treceivableDetail =new TreceivableDetail();
				treceivableDetail.setArCode(tar1.getArCode());
				treceivableDetail.setRowNumber(tarDetail.getRowNumber());
				treceivableDetail.setBrand(tarDetail.getBrand());
				treceivableDetail.setModel(tarDetail.getModel());
				treceivableDetail.setArea(tarDetail.getArea());
				treceivableDetail.setCarId(tarDetail.getCarId());
				treceivableDetail.setTcar(tarDetail.getCar());
				treceivableDetail.setFrameNumber(tarDetail.getFrameNumber());
				treceivableDetail.setFixedExpense(tarDetail.getFixedExpense());
				treceivableDetail.setExpenseItem(tarDetail.getExpenseItem());
				treceivableDetail.setReceivedLeft(tarDetail.getReiLeft());
				treceivableDetail.setArAmount(tarDetail.getApAmount());
				tarDetail.setArCode(tar1.getArCode());
				treceivableDetailList.add(treceivableDetail);
			}
		}
		return treceivableDetailList;
	}

	/**
	 * 获得费用项目列表
	 *
	 */
	@RequestMapping(value = "getExpenseItem")
	@ResponseBody
	public List<FixedExpense> getExpenseItem() {
		List<FixedExpense> list = fixedExpenseService.findList(new FixedExpense());
		return list;
	}

	/**
	 * 更新状态
	 * @param arId
	 * @return
	 */
	@RequestMapping(value = "updateBill")
	@ResponseBody
	public String updateBill(@RequestParam("arId") String arId,@RequestParam("sta") String sta){
		Tar tar = tarService.get(new Tar(arId));
		tar.setBillStatus(sta);
		if("3".equals(sta)){
			tar.setStatus1("2");
		}
		tarService.update(tar);
		return "操作成功！";
	}

}